package org.lanqiao.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.lanqiao.orm.EleInfo;
import org.lanqiao.orm.EleManager;
import org.lanqiao.orm.EleOpeartor;
import org.lanqiao.orm.EleUser;
import org.lanqiao.orm.LoginBean;
import org.lanqiao.service.ICheckLogin;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping(value = "/user")
public class LoginControl {
	@Resource
	private ICheckLogin loginService;

	@RequestMapping(value = "login.action")
	public String login(HttpSession session, LoginBean bean, Model model) {
		String role = bean.getRole();
		String strlogin = "../../login";
		session.setAttribute("role", role);
		if (role!=null&&role.equals("1")) {
			// 验证管理员登录
			EleManager manager = loginService.ManagerLogin(bean);
			if (manager != null) {
				strlogin = "logined";
				session.setAttribute("manager", manager);
			}
		} else if (role!=null&&role.equals("2")) {
			// 验证操作员登录
			EleOpeartor op = loginService.OpeartorLogin(bean);
			if (op != null) {
				strlogin = "logined";
				session.setAttribute("opeartor", op);
			}
		} else if (role!=null&&role.equals("3")) {
			// 验证用户登录
			EleUser user = loginService.UserLogin(bean);
			if (user != null) {
				strlogin = "logined";
				session.setAttribute("user", user);
			}
		}
		if(strlogin.equals("logined")){
			session.setAttribute("islogin", "success");
			EleInfo info=loginService.selectInfo();
			session.setAttribute("newInfo", info);
		}
		return strlogin;
	}
	
	@RequestMapping(value="exit.action")
	public String exit(HttpSession session){
		session.invalidate();
		return "../../login";
	}
}
